package com.stu.action.sys;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.stu.action.base.BaseAction;
import com.stu.entity.sys.Log4jBean;
import com.stu.service.sys.ILog4jService;
import com.stu.utils.CommonUtils;
import com.stu.utils.PageBean;

@Controller
@Scope("prototype")
public class Log4jAction extends BaseAction<Log4jBean, ILog4jService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String list(){
		String hql="from Log4jBean where 1=1 ";
		
		String st=getRequest().getParameter("startTime");
		String et=getRequest().getParameter("endTime"); 
		String tp=getRequest().getParameter("tp"); 
		Map<String,Object>params=new HashMap<String,Object>();
		 
		if(StringUtils.isNotBlank(st)){
			hql+=" and logdate > :st";
			params.put("st", st); 
		} 
		if(StringUtils.isNotBlank(et)){
			hql+=" and logdate < :et";
			params.put("et", et);
		}
		if(StringUtils.isNotBlank(tp)){
			hql+=" and priority = :tp";
			params.put("tp", tp.trim()); 
		}
		hql+=" order by createTime desc ";
		
		List<Log4jBean>logList=super.findByHql(hql,params);
		PageBean pageBean=new PageBean(page, getAccount(), rows);

		getRequest().setAttribute("startTime", st);
		getRequest().setAttribute("endTime", et); 
		
//		CommonUtils.getValueStack().getContext().put("pageBean", pageBean);
//		CommonUtils.getValueStack().getContext().put("curData", logList);

		JSONObject j=new JSONObject();
		j.put("data", logList);
		j.put("success", true);
		j.put("pageBean", pageBean);
		try {
			CommonUtils.writeJson(j.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "list";
	}
	
//	public String search(){
//		String hql="from Log4jBean where 1=1 ";
//		
//		String st=getRequest().getParameter("startTime");
//		String et=getRequest().getParameter("endTime"); 
//		String tp=getRequest().getParameter("tp"); 
//		Map<String,Object>params=new HashMap<String,Object>();
//		 
//		if(StringUtils.isNotBlank(st)){
//			hql+=" and logdate > :st";
//			params.put("st", st); 
//		} 
//		if(StringUtils.isNotBlank(et)){
//			hql+=" and logdate < :et";
//			params.put("et", et);
//		}
//		if(StringUtils.isNotBlank(tp)){
//			hql+=" and priority = :tp";
//			params.put("tp", tp.trim()); 
//		}
//		hql+=" order by createTime desc ";
//		
//		List<Log4jBean>logList=super.findByHql(hql,params);
//		PageBean pageBean=new PageBean(page, getAccount(), rows);
//
//		CommonUtils.getValueStack().getContext().put("pageBean", pageBean);
//		CommonUtils.getValueStack().getContext().put("curData", logList);
//
//		return "list";
//	}
//	
	
	
	/** 
	 * 根据查询条件获取记录数
	 * @author lijian
	 * @return
	 */
	private int getAccount(){
		String hql="select count(*) from Log4jBean where 1=1 ";
		
		String st=getRequest().getParameter("startTime"); 
		String et=getRequest().getParameter("endTime"); 
		String tp=getRequest().getParameter("tp"); 
		Map<String,Object>params=new HashMap<String,Object>();
		 
		if(StringUtils.isNotBlank(st)){
			hql+=" and logdate > :st";
			params.put("st", st); 
		} 
		if(StringUtils.isNotBlank(et)){
			hql+=" and logdate < :et"; 
			params.put("et", et);
		}
		if(StringUtils.isNotBlank(tp)){
			hql+=" and priority = :tp";
			params.put("tp", tp.trim()); 
		}
		hql+=" order by createTime desc ";
		return super.getAccount(hql, params);
	}
	 
	public String details(){
		String id=getRequest().getParameter("id");
		Log4jBean log=super.findById(id);
		CommonUtils.getValueStack().getContext().put("loginfo", log);
		return "details";
	}
	
	/**
	 * 删除日志
	 * @author lijian
	 *
	 */
	public void deleteLogsByIds(){
		boolean flag=super.deleteByIds();
		JSONObject json=new JSONObject();
		json.put("success", flag); 
		try { 
			CommonUtils.writeJson(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/* 页面属性 */
	private Date startTime;
	private Date endTime;
	private String tp;

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getTp() {
		return tp;
	}

	public void setTp(String tp) {
		this.tp = tp;
	} 

	
	

}
